package com.bugbuff.dubbo.server.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.bugbuff.common.pojo.PageResult;
import com.bugbuff.dubbo.server.pojo.FrozenUser;
import com.bugbuff.dubbo.server.service.UserManageService;
import com.bugbuff.dubbo.server.vo.UserManageInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("management/manage")
@Slf4j
public class UserManageController {

    @Autowired
    private UserManageService userManageService;

    /**
     * 查找用户详情
     *
     * @param userID
     * @return
     */
    @GetMapping("users/{userID}")
    public ResponseEntity<UserManageInfoVo> baseUserInfo(@PathVariable("userID") Long userID) {

        try {
            UserManageInfoVo vo = userManageService.findUserInfo(userID);
            if (ObjectUtil.isNotEmpty(vo))
                return ResponseEntity.ok(vo);
        } catch (Exception e) {
            log.warn("查找用户基本资料失败！~");
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查找用户分页数据
     *
     * @param pagesize
     * @param page
     * @param id
     * @param nickname
     * @param city
     * @return
     */
    @GetMapping("users")
    public ResponseEntity<PageResult> findUserInfoByPage(@RequestParam(value = "pagesize", required = false, defaultValue = "10") Integer pagesize,
                                                         @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                                         @RequestParam Long id,
                                                         @RequestParam String nickname,
                                                         @RequestParam String city) {

        PageResult pageResult = null;
        try {
            pageResult = userManageService.findUserInfoByPage(page, pagesize, id, nickname, city);
            if (ObjectUtil.isNotEmpty(pageResult))
                return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            log.warn("查找用户分页基本资料失败！~，pageResult = {}", pageResult);
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 用户冻结操作
     */
//    @PostMapping("freeze")
//    public ResponseEntity<String> freezeUser(Integer userId,
//                                             Integer freezingTime,
//                                             Integer freezingRange,
//                                             String reasonsForFreezing,
//                                             String frozenRemarks) {
    @PostMapping("users/freeze")
    public ResponseEntity<String> freezeUser(@RequestBody Map<String, Object> param) {

        try {
            FrozenUser frozenUser = new FrozenUser();
            frozenUser.setUserId(Convert.toInt(param.get("userId")));
            frozenUser.setFreezingTime(Convert.toInt(param.get("freezingTime")));
            frozenUser.setFreezingRange(Convert.toInt(param.get("freezingRange")));
            frozenUser.setReasonsForFreezing(param.get("reasonsForFreezing").toString());
            frozenUser.setFrozenRemarks(param.get("frozenRemarks").toString());

            Boolean result = userManageService.freezeUser(frozenUser);
            if (result)
                return ResponseEntity.ok("操作成功");
        } catch (Exception e) {
            log.warn("用户冻结操作失败！~");
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 用户解冻操作
     */
//    @PostMapping("unfreeze")
//    public ResponseEntity<String> unfreezeUser(Integer userId,
//                                               String reasonsForThawing) {
    @PostMapping("users/unfreeze")
    public ResponseEntity<String> unfreezeUser(@RequestBody Map<String, Object> param) {

        try {
            FrozenUser frozenUser = new FrozenUser();
            frozenUser.setUserId(Convert.toInt(param.get("userId")));
            frozenUser.setReasonsForThawing(param.get("reasonsForThawing").toString());

            Boolean result = userManageService.unfreezeUser(frozenUser);
            if (result)
                return ResponseEntity.ok("操作成功");
        } catch (Exception e) {
            log.warn("用户解冻操作失败！~");
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


}
